package com.hjj.springboot.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjj.springboot.config.LogType;
import com.hjj.springboot.config.SystemLogs;
import com.hjj.springboot.entity.*;
import com.hjj.springboot.service.IPrombleService;
import com.hjj.springboot.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/promble")
public class AdminPrombleController {

    @Autowired
    private IPrombleService prombleService;

    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        String query=pageBean.getQuery().trim();
        Page<Promble> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Promble> pageResult = prombleService.page(page,new QueryWrapper<Promble>().like(StringUtil.isNotEmpty(query),"promble",query));
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("prombleList",pageResult.getRecords());
        return R.ok(map);
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Promble promble=prombleService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("promble",promble);
        return R.ok(map);
    }

    @SystemLogs(operation = "常见问题",type = LogType.UPDATE)
    @RequestMapping("/save")
    public R save(@RequestBody Promble promble){
        if(promble.getId()==null || promble.getId()==-1){
            prombleService.save(promble);
        }else {
            prombleService.saveOrUpdate(promble);
        }
        return R.ok();
    }

    @SystemLogs(operation = "常见问题",type = LogType.DELETE)
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id) {
            prombleService.removeById(id);
            return R.ok();

    }

    //    批量删除
    @SystemLogs(operation = "常见问题",type = LogType.BATCH_DELETE)
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        prombleService.removeByIds(ids);
        return R.ok();
    }

    @GetMapping("/export")
    public void export(@RequestParam(required = false) String query, HttpServletResponse response)   throws Exception {
        String queryStr=query.trim();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("常见问题", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Promble> prombleList = prombleService.list(new QueryWrapper<Promble>().like(StringUtil.isNotEmpty(queryStr),"promble",query));
        EasyExcel.write(response.getOutputStream(),Promble.class)
                .sheet("常见问题")
                .doWrite(prombleList);
    }
}
